﻿/******************************************************************************
 * 创建者：gtts
 * 创建日期：2011.3.28
 * 类名：PictureCartoon
 * 说明：图像的简单动画
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GTTSOfLibrary.PictureHandle
{
    /// <summary>
    /// 图像的简单动画处理
    /// </summary>
    public class PictureCartoon
    {
        #region 单例
        private static PictureCartoon _PictureCartoon;
        private PictureCartoon()
        { }
        public static PictureCartoon Instance
        {
            get
            {
                if (_PictureCartoon == null)
                {
                    _PictureCartoon = new PictureCartoon();
                }
                return _PictureCartoon;
            }
        }
        #endregion

        #region 垂直百叶窗效果
        /// <summary>
        /// 垂直百叶窗效果
        /// </summary>
        /// <param name="picBox">在此PictureBox上显示</param>
        /// <param name="bitmap">要变换的图片</param>
        /// <param name="num">窗的数量</param>
        /// <param name="intervalTime">间隔时间</param>
        public void VerticalWindowShades(System.Windows.Forms.PictureBox picBox, Bitmap bitmap, int num, int intervalTime)
        {
            Bitmap bm = (Bitmap)bitmap.Clone();
            int dw = bm.Width / num;
            int dh = bm.Height;
            Graphics g = picBox.CreateGraphics();
            g.Clear(Color.Gray);
            Point[] myPoint = new Point[num];
            for (int i = 0; i < num; i++)
            {
                myPoint[i].Y = 0;
                myPoint[i].X = i * dw;
            }

            Bitmap bm1 = new Bitmap(bm.Width, bm.Height);
            for (int i = 0; i < dw; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    for (int k = 0; k < dh; k++)
                    {
                        bm1.SetPixel(myPoint[j].X + i, myPoint[j].Y + k,
                            bm.GetPixel(myPoint[j].X + i, myPoint[j].Y + k));
                    }
                }
                picBox.Refresh();
                picBox.Image = bm1;
                System.Threading.Thread.Sleep(intervalTime);
            }

            //清理
            g.Dispose();
        }
        #endregion

        #region 水平百叶窗效果
        /// <summary>
        /// 水平百叶窗效果
        /// </summary>
        /// <param name="picBox">在此PictureBox上显示</param>
        /// <param name="bitmap">要变换的图片</param>
        /// <param name="num">窗的数量</param>
        /// <param name="intervalTime">间隔时间</param>
        public void HorizontalWindowShades(System.Windows.Forms.PictureBox picBox, Bitmap bitmap, int num, int intervalTime)
        {
            Bitmap bm = (Bitmap)bitmap.Clone();
            int dw = bm.Width;
            int dh = bm.Height / num;
            Graphics g = picBox.CreateGraphics();
            g.Clear(Color.Gray);
            Point[] myPoint = new Point[num];
            for (int i = 0; i < num; i++)
            {
                myPoint[i].Y = i * dh;
                myPoint[i].X = 0;
            }

            Bitmap bm1 = new Bitmap(bm.Width, bm.Height);
            for (int i = 0; i < dh; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    for (int k = 0; k < dw; k++)
                    {
                        bm1.SetPixel(myPoint[j].X + k, myPoint[j].Y + i,
                            bm.GetPixel(myPoint[j].X + k, myPoint[j].Y + i));
                    }
                }
                picBox.Refresh();
                picBox.Image = bm1;
                System.Threading.Thread.Sleep(intervalTime);
            }

            //清理
            g.Dispose();
        }
        #endregion

        #region 马赛克显示
        /// <summary>
        /// 马赛克显示
        /// </summary>
        /// <param name="picBox">在此PictureBox上显示</param>
        /// <param name="bitmap">要变换的图片</param>
        /// <param name="num">马赛克的数量</param>
        public void MosaicShow(System.Windows.Forms.PictureBox picBox, Bitmap bitmap, int num)
        {
            Bitmap bm = bitmap.Clone() as Bitmap;
            int dw = bitmap.Width / num;
            int dh = bitmap.Height / num;
            Graphics g = picBox.CreateGraphics();
            g.Clear(Color.Gray);
            Point[] myPoint = new Point[num * num];
            for (int x = 0; x < num; x++)
            {
                for (int y = 0; y < num; y++)
                {
                    myPoint[x * num + y].X = x * dw;
                    myPoint[x * num + y].Y = y * dh;
                }
            }
            Bitmap bm1 = new Bitmap(bm.Width, bm.Height);

            for (int i = 0; i < 4 * num * num; i++)
            {
                Random myRandom = new Random();
                int iPos = myRandom.Next(num * num);
                for (int m = 0; m < dw; m++)
                    for (int n = 0; n < dh; n++)
                    {
                        bm1.SetPixel(myPoint[iPos].X + m, myPoint[iPos].Y + n, bm.GetPixel(myPoint[iPos].X + m, myPoint[iPos].Y + n));
                    }
                picBox.Refresh();
                picBox.Image = bm1;
            }

            for (int i = 0; i < num * num; i++)
            {
                for (int m = 0; m < dw; m++)
                {
                    for (int n = 0; n < dh; n++)
                    {
                        bm1.SetPixel(myPoint[i].X + m, myPoint[i].Y + n, bm.GetPixel(myPoint[i].X + m, myPoint[i].Y + n));
                    }
                }
            }
            picBox.Refresh();
            picBox.Image = bm1;
            g.Dispose();
        }
        #endregion
    }
}
